package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.order.api.ProductApi;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {

    // 删除购物车商品，调用商品服务
    @Autowired
    ProductApi productApi;

    @Autowired
    RedissonClient redissonClient;


    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        ClearCartItemRequest clearCartItemRequest = new ClearCartItemRequest();

        clearCartItemRequest.setUserId(createOrderContext.getUserId());
        clearCartItemRequest.setProductIds(createOrderContext.getBuyProductIds());

        ClearCartItemResponse clearCartItemResponse = productApi.clearCartItemByUserID(clearCartItemRequest);
        // if (!clearCartItemResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
        //     throw new BizException("清空选中商品购物车失败！");
        // }
        return true;
    }
}
